package com.weather.ui.mvp;

import android.util.Log;

import com.weather.bean.AccurealFeel;
import com.weather.bean.AccurealFeelResult;
import com.weather.bean.AppIndexAll;
import com.weather.bean.IndexIcon;
import com.weather.bean.KeyWord;
import com.weather.bean.MinuteNew;
import com.weather.bean.Yinpin;
import com.weather.bean.YinpinResult;
import com.weather.net.NetConfig;
import com.weather.net.NetRequest;
import com.weather.net.NetRespone;
import com.weather.utils.FileUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by sunshine on 2017/3/7.
 */

public class ModelImp implements Constant.Model {
    private static final String TAG = "====ModelImp====";

    @Override
    public void getMinuteNew(Map<String,String> map,final NetRespone respone) {
        final String TAG = "getMinuteNew";
        if (map != null){
            map.put("FKEY", NetConfig.FKEY);
        }
        Observable<MinuteNew> observable = NetRequest.getInstance().getApi().getMinuteNew(map);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<MinuteNew>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, e.getMessage());
                    }

                    @Override
                    public void onNext(MinuteNew result) {
                        respone.success(result);
                    }
                });
    }

    @Override
    public void getAppIndexAll(final NetRespone respone) {
        final String TAG = "getAppIndexAll";
        Map<String, String> map = NetConfig.getBaseParams();
        map.put("proid", NetConfig.PROID);
        map.put("qudao", NetConfig.QUDAO);
        Observable<AppIndexAll<List<IndexIcon>>> appIndexAll = NetRequest.
                getInstance().getApi().getAppIndexAll(map);
        appIndexAll.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<AppIndexAll<List<IndexIcon>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "走onError");
                        Log.e(TAG, e.getMessage());
                    }

                    @Override
                    public void onNext(AppIndexAll<List<IndexIcon>> result) {
                        IndexIcon icon = result.getIndexicon().get(0);
                        if (icon == null) {
                            Log.e(TAG, "icon == null");
                        }
                        respone.success(icon);
                    }
                });
    }

    @Override
    public void getAccurealFeel(final NetRespone respone) {
        final String TAG = "getAccurealFeel";
        Map<String, String> map = NetConfig.getBaseParams();
        map.put("data", NetConfig.DATA);
        Observable<AccurealFeel<List<AccurealFeelResult>>> observable = NetRequest.getInstance().
                getApi().getAccurealFeel(map);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<AccurealFeel<List<AccurealFeelResult>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "走onError");
                        Log.e(TAG, e.getMessage());
                    }

                    @Override
                    public void onNext(AccurealFeel<List<AccurealFeelResult>> result) {
                        respone.success(result.getResult().get(0));
                    }
                });
    }

    @Override
    public void getYinpin(final NetRespone respone) {
        final String TAG = "getYinpin";
        Map<String, String> map = NetConfig.getBaseParams();
        map.put("data", NetConfig.PROID);
        Observable<Yinpin<List<YinpinResult>>> observable =
                NetRequest.getInstance().getApi().getYinpin(map);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Yinpin<List<YinpinResult>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "走onError");
                        Log.e(TAG, e.getMessage());
                    }

                    @Override
                    public void onNext(Yinpin<List<YinpinResult>> result) {
                        YinpinResult yinpinResult = result.getYinpin().get(0);
                        respone.success(yinpinResult);
                    }
                });
    }

    @Override
    public void getKeywordNew(final NetRespone respone) {
        final String TAG = "getKeywordNew";
        Observable<List<KeyWord>> observable = NetRequest.getInstance().getApi().getkeywordnew(NetConfig.getBaseParams());
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<KeyWord>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "走onError");
                        Log.e(TAG, e.getMessage());
                    }

                    @Override
                    public void onNext(List<KeyWord> keyWords) {
                        try {
                            JSONObject obj = new JSONObject(keyWords.get(0).getKeywordcontent());
                            JSONObject json = (JSONObject) obj.get("0");
                            respone.success(json.getString("a"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @Override
    public void downYinpin(final String url, final NetRespone respone) {
        OkHttpClient client = NetRequest.getInstance().getClient();
        Request request = new Request.Builder()
                .url(NetConfig.BASE_URL_AUDIO+url)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response result) throws IOException {
                File file = new File(FileUtils.getAudioCacheFile(),url);
                if (!file.exists()){
                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(result.body().bytes());
                    fos.close();
                }
                respone.success(true);
            }
        });
    }
}
